package com.minivision.fdiot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.minivision.fdiot.interceptor.ApiAuthInterceptor;
import com.minivision.fdiot.interceptor.RateLimitingInterceptor;
import com.minivision.fdiot.rate.config.RateLimitingProperties;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
	
	@Autowired
	private ApiAuthInterceptor apiAuthInterceptor;
	
	@Autowired
	private RateLimitingInterceptor rateInterceptor;

	@Autowired
    private RateLimitingProperties rateLimitingProperties;

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
	    //鉴权拦截
		registry.addInterceptor(apiAuthInterceptor).addPathPatterns("/api/**");
		//限流拦截
		if (rateLimitingProperties.isEnabled()) {
          registry.addInterceptor(rateInterceptor)
                  .addPathPatterns("/api/**");
        }
	}
	
}
